package com.scqkxx.gamebox.activity.myself;

import android.app.Dialog;
import android.content.ContentValues;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.text.Editable;
import android.text.InputFilter;
import android.text.Spanned;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.Toast;

import com.avos.avoscloud.AVException;
import com.avos.avoscloud.AVObject;
import com.avos.avoscloud.AVQuery;
import com.avos.avoscloud.AVUser;
import com.avos.avoscloud.FindCallback;
import com.avos.avoscloud.SaveCallback;
import com.scqkxx.gamebox.R;
import com.scqkxx.gamebox.activity.activities.utils.ActivityUtil;
import com.scqkxx.gamebox.activity.activities.utils.GlideUtils;
import com.scqkxx.gamebox.activity.activities.view.TitleView;
import com.scqkxx.gamebox.activity.myself.adapter.GoodsAdapter;
import com.scqkxx.gamebox.activity.myself.adapter.SpaceAdapter;
import com.scqkxx.gamebox.activity.myself.address.SpaceBean;
import com.scqkxx.gamebox.activity.myself.spaceUtils.SpaceUtils;
import com.scqkxx.gamebox.activity.space.SearchActivity;
import com.scqkxx.gamebox.bean.EvenBusBean;
import com.scqkxx.gamebox.databinding.ActivityMyselfEditNicknameBinding;
import com.scqkxx.gamebox.databinding.ActivityMyselfInforBinding;
import com.scqkxx.gamebox.utils.DialogUtils;
import com.scqkzqtz.base.base.BaseActivity;
import com.scqkzqtz.base.library.utils.ToastUtils;
import com.scwang.smartrefresh.layout.util.DensityUtil;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import io.reactivex.Observable;
import io.reactivex.functions.Function;

/**
 * 空间详情
 * Created by hef on 2018/8/13.
 */

public class MyselfEditNicknameActivity extends BaseActivity<ActivityMyselfEditNicknameBinding> implements View.OnClickListener {

    private GoodsAdapter mGoodsAdapter;
    private List<AVObject> mListData = null;
    private int spaceState = 1;
    private int belongGroupId;

    private View inflate;
    private Button choosePhoto;
    private Button takePhoto;
    private Button cancel;
    private Dialog dialog;
    private String objectId;
    private int spaceId;
    private String spaceName = "";
    private String imageUrl = "" ;
    private String goodsName;


    @Override
    public int setLayoutId() {
        return R.layout.activity_myself_edit_nickname;
    }

    @Override
    public void init() {
        EventBus.getDefault().register(this);
        spaceState = getIntent().getIntExtra("spaceState", 1);
        belongGroupId = getIntent().getIntExtra("belongGroupId", -1);
        objectId = getIntent().getStringExtra("objectId");
        spaceId = getIntent().getIntExtra("spaceId", 0);
        if (spaceState == 2) {
            binding.linPostions.setVisibility(View.GONE);
            binding.goodsAndpostions.setText("物品");
            binding.tuding.setVisibility(View.VISIBLE);
        } else {
            binding.linPostions.setVisibility(View.VISIBLE);
//            binding.tvPostions.setText(positionNum + "");
            binding.goodsAndpostions.setText("位置");
            binding.tuding.setVisibility(View.GONE);
        }

        binding.tvTitleView.getIv_setting().setImageResource(R.mipmap.space_edit_icon);
        binding.tuding.setOnClickListener(v -> {
            ContentValues contentValues = new ContentValues();
            contentValues.put("imageUrl", imageUrl);
            Log.e("TAG" , imageUrl);
            contentValues.put("objectId", objectId);
            contentValues.put("belongGroupId", belongGroupId);
            contentValues.put("spaceName", spaceName);
            ActivityUtil.switchTo(MyselfEditNicknameActivity.this, SettingAboutUsActivity.class, contentValues);
        });
        binding.tvTitleView.setOnTitleClickListener(mTitleType -> {
            if (TitleView.CLICK_BACK == mTitleType) {
                finish();
            } else if (TitleView.CLICK_SETTING == mTitleType) {
                //编辑
                inflate = LayoutInflater.from(MyselfEditNicknameActivity.this).inflate(R.layout.eait_show_dialog, null);
                dialog = DialogUtils.showBottonDialog(MyselfEditNicknameActivity.this, inflate);
                choosePhoto = (Button) inflate.findViewById(R.id.choosePhoto);
                takePhoto = (Button) inflate.findViewById(R.id.takePhoto);
                cancel = (Button) inflate.findViewById(R.id.btn_cancel);
                choosePhoto.setOnClickListener(MyselfEditNicknameActivity.this);
                takePhoto.setOnClickListener(MyselfEditNicknameActivity.this);
                cancel.setOnClickListener(MyselfEditNicknameActivity.this);
                dialog.show();
            }
        });
        binding.serch.setOnClickListener(v -> {
            //跳转搜索
            ActivityUtil.switchTo(MyselfEditNicknameActivity.this, SearchActivity.class);
        });
        initView();
        initDate();
    }

    private void initView() {
        mListData = new ArrayList<>();
        mGoodsAdapter = new GoodsAdapter(R.layout.item_goods, mListData, spaceState);
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
        binding.mRecyclerView.setLayoutManager(linearLayoutManager);
        ViewGroup parentView = (ViewGroup) this.getWindow().getDecorView();
        View view = getLayoutInflater().inflate(R.layout.item_end_goods, parentView, false);
        mGoodsAdapter.addFooterView(view);
        view.setOnClickListener(v -> {
            ContentValues contentValues = new ContentValues();
            if (spaceState == 1) {
                //位置添加
                contentValues.put("spaceState", 3);
                contentValues.put("spaceId", spaceId);
                contentValues.put("spaceName", spaceName);
            } else {
                //物品添加
                contentValues.put("goodsName", goodsName);
                contentValues.put("spaceId", objectId);
                contentValues.put("spaceState", 5);
            }
            ActivityUtil.switchTo(MyselfEditNicknameActivity.this, MyselfInforActivity.class, contentValues);
        });
        binding.mRecyclerView.setAdapter(mGoodsAdapter);
        binding.mRecyclerView.setOverScrollMode(View.OVER_SCROLL_NEVER);
    }


    private void initDate() {
        mListData.clear();
        if (spaceState == 1) {
            //位置列表
            DialogUtils.showDialog(this);
            AVQuery<AVObject> query = AVQuery.getQuery("Position");
            query.whereEqualTo("belongGroupId", belongGroupId);
            query.whereEqualTo("spaceId", objectId);
            query.findInBackground(new FindCallback<AVObject>() {
                @Override
                public void done(List<AVObject> list, AVException e) {
                    if (e == null) {
                        mListData.addAll(list);
                        mGoodsAdapter.setNewData(mListData);
                    }
                    DialogUtils.dismiss();
                }
            });

            AVQuery<AVObject> spaceQuery = AVQuery.getQuery("Space");
            spaceQuery.whereEqualTo("objectId", objectId );
            spaceQuery.findInBackground(new FindCallback<AVObject>() {
                @Override
                public void done(List<AVObject> avObjects, AVException avException) {
                    if (avException == null) {
                        if ( avObjects.size() > 0 ) {
                            AVObject avObject = avObjects.get(0);
                            spaceName = (String) avObject.get("name");
                            imageUrl = (String) avObject.get("avatar");
                            binding.tvTitleView.getTv_name().setText((String) avObject.get("name"));
                            binding.spaceName.setText((String) avObject.get("name"));
                            binding.username.setText("创建者：" + avObject.get("belongUserName"));
                            binding.createTime.setText("创建时间：" + new SimpleDateFormat("yyyy年MM月dd日 HH:mm").format(avObject.getCreatedAt()));
                            binding.tvGoods.setText(avObject.get("goodsNum") + "");
                            binding.tvMenbers.setText(avObject.get("membersNum") + "");
                            binding.tvPostions.setText(avObject.get("positionNum") + "");
                            GlideUtils.setImageeCircle(MyselfEditNicknameActivity.this, (String) avObject.get("avatar"), binding.image);
                        }
                    }
                }
            });
        } else {
            //物品列表
            DialogUtils.showDialog(this);
            AVQuery<AVObject> query = AVQuery.getQuery("Goods");
            query.whereEqualTo("belongGroupId", belongGroupId);
            query.whereEqualTo("positionId", objectId);
            query.whereEqualTo("isDisable" ,0);
            query.findInBackground(new FindCallback<AVObject>() {
                @Override
                public void done(List<AVObject> list, AVException e) {
                    if (e == null) {
                        mListData.addAll(list);
                        mGoodsAdapter.setNewData(mListData);
                    }
                    DialogUtils.dismiss();
                }
            });

            AVQuery<AVObject> spaceQuery = AVQuery.getQuery("Position");
            spaceQuery.whereEqualTo("objectId", objectId);
            spaceQuery.findInBackground(new FindCallback<AVObject>() {
                @Override
                public void done(List<AVObject> avObjects, AVException avException) {
                    if (avException == null) {
                        if (avObjects.size() > 0 ) {
                            AVObject avObject = avObjects.get(0);
                            spaceName = (String) avObject.get("spaceName");
                            goodsName = (String) avObject.get("name");
                            imageUrl = avObject.get("avatar").toString();
                            binding.tvTitleView.getTv_name().setText((String) avObject.get("name"));
                            binding.spaceName.setText((String) avObject.get("name"));
                            binding.username.setText("创建者：" + avObject.get("belongUserName"));
                            binding.createTime.setText("创建时间：" + new SimpleDateFormat("yyyy年MM月dd日 HH:mm").format(avObject.getCreatedAt()));
                            binding.tvGoods.setText(avObject.get("goodsNum") + "");
                            binding.tvMenbers.setText(avObject.get("membersNum") + "");
                            binding.tvPostions.setText(avObject.get("positionNum") + "");
                            GlideUtils.setImageeCircle(MyselfEditNicknameActivity.this, (String) avObject.get("avatar"), binding.image);
                        }
                    }
                }
            });
        }
        DialogUtils.dismiss();
    }


    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.takePhoto:
                ContentValues contentValues = new ContentValues();
                contentValues.put("spaceName", spaceName);
                contentValues.put("imageUrl", imageUrl);
                if (spaceState == 1) {
                    //修改空间数据
                    contentValues.put("belongGroupId", Integer.valueOf(objectId));
                    contentValues.put("spaceState", 2);
                    ActivityUtil.switchTo(this, MyselfInforActivity.class, contentValues);
                } else {
                    //修改物品数据
                    contentValues.put("belongGroupId", Integer.valueOf(objectId));
                    contentValues.put("goodsName" , goodsName );
                    contentValues.put("spaceState", 4 );
                    ActivityUtil.switchTo(this, MyselfInforActivity.class, contentValues);
                }
                if (dialog != null) dialog.dismiss();
                break;
            case R.id.choosePhoto:
                //删除
                if (dialog != null) dialog.dismiss();
                try {
                    SpaceUtils.deleteSpaceAndGoods(spaceState, this, objectId);
                } catch (AVException e) {
                    e.printStackTrace();
                }
                break;
            case R.id.btn_cancel:
                if (dialog != null) dialog.dismiss();
        }
    }


    @Subscribe
    public void changeDataBean(EvenBusBean eventBus) throws AVException {
        if (eventBus.getState() == 101) {
            initDate();
        }
    }


    @Override
    public void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }
}
